Conditions | 1 |
Paths | 49 |
Total Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
81 | inji.onLoad(function () { |
||
82 | |||
83 | //plugin bootstrap minus and plus |
||
84 | //http://jsfiddle.net/laelitenetwork/puJ6G/ |
||
85 | $('body').on('click', '.btn-number', function (e) { |
||
86 | e.preventDefault(); |
||
87 | |||
88 | var fieldName = $(this).data('field'); |
||
89 | var type = $(this).data('type'); |
||
90 | var input = $("input[name='" + fieldName + "']"); |
||
91 | var currentVal = parseFloat(input.val()); |
||
92 | if (!isNaN(currentVal)) { |
||
93 | if (type == 'minus') { |
||
94 | |||
95 | if (currentVal > input.attr('min')) { |
||
96 | input.val(currentVal - 1).change(); |
||
97 | } |
||
98 | if (parseFloat(input.val()) == input.attr('min')) { |
||
99 | $(this).attr('disabled', true); |
||
100 | } |
||
101 | |||
102 | } else if (type == 'plus') { |
||
103 | |||
104 | if (currentVal < input.attr('max')) { |
||
105 | input.val(currentVal + 1).change(); |
||
106 | } |
||
107 | if (parseFloat(input.val()) == input.attr('max')) { |
||
108 | $(this).attr('disabled', true); |
||
109 | } |
||
110 | |||
111 | } |
||
112 | } else { |
||
113 | input.val(0); |
||
114 | } |
||
115 | }); |
||
116 | $('body').on('focusin', '.input-number', function () { |
||
117 | $(this).data('oldValue', $(this).val()); |
||
118 | }); |
||
119 | $('body').on('change', '.input-number', function () { |
||
120 | |||
121 | var minValue = parseFloat($(this).attr('min')); |
||
122 | var maxValue = parseFloat($(this).attr('max')); |
||
123 | var valueCurrent = parseFloat($(this).val()); |
||
124 | |||
125 | var name = $(this).attr('name'); |
||
126 | if (valueCurrent >= minValue) { |
||
127 | $(".btn-number[data-type='minus'][data-field='" + name + "']").removeAttr('disabled') |
||
128 | } else { |
||
129 | alert('Нельзя заказать меньше ' + minValue); |
||
130 | $(this).val($(this).data('oldValue')); |
||
131 | } |
||
132 | if (valueCurrent <= maxValue) { |
||
133 | $(".btn-number[data-type='plus'][data-field='" + name + "']").removeAttr('disabled') |
||
134 | } else { |
||
135 | alert('Извините, но больше нету'); |
||
136 | $(this).val($(this).data('oldValue')); |
||
137 | } |
||
138 | |||
139 | |||
140 | }); |
||
141 | |||
142 | $('body').on('keydown', ".input-number", function (e) { |
||
143 | // Allow: backspace, delete, tab, escape, enter and . |
||
144 | if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 || |
||
145 | // Allow: Ctrl+A |
||
146 | (e.keyCode == 65 && e.ctrlKey === true) || |
||
147 | // Allow: home, end, left, right |
||
148 | (e.keyCode >= 35 && e.keyCode <= 39)) { |
||
149 | // let it happen, don't do anything |
||
150 | return; |
||
151 | } |
||
152 | // Ensure that it is a number and stop the keypress |
||
153 | if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) { |
||
154 | e.preventDefault(); |
||
155 | } |
||
156 | }); |
||
157 | |||
158 | }) |
||
159 |